package com.example.comiccenter.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.comiccenter.domain.Admin;
import com.example.comiccenter.service.AdminService;
import com.example.comiccenter.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.example.comiccenter.utils.Consts.CODE;
import static com.example.comiccenter.utils.Consts.MSG;

//管理员账户的增删改查
@RestController
@RequestMapping(value = "/admin",method= RequestMethod.POST)
public class AdminController {
    @Autowired
    private AdminService adminService;


    //登录接口
    @PostMapping(value="/login")
    public Object login(@RequestBody Admin admin) {
        JSONObject json =new JSONObject();
        String adminname = admin.getAdminname();
        String adminpassword = admin.getPassword();
        boolean flag = adminService.verifyPassword(adminname, adminpassword);
        if (flag) {
            String token = JWTUtil.getToken(adminname);
            json.put(CODE,"1");
            json.put("TOKEN",token);
            json.put(MSG,"登陆成功");

        }
        else {
            json.put(CODE,"0");
            json.put(MSG,"登陆失败");
        }
        return json;
    }
    //添加管理员接口
    @PostMapping(value = "/insert")
    public Object insertAdmin(@RequestBody Admin admins)
    {
        JSONObject json = new JSONObject();
        if (admins.getPassword() == null){
            admins.setPassword("123456");
        }
        int result= adminService.insertAdmin(admins);//预设密码
        System.out.println(result);
        if (result == 0)
        {
            json.put(CODE,"0");
            json.put(MSG,"添加失败");
        }
        else {
            json.put(CODE,"1");
            json.put(MSG,"添加成功");
        }
        return json;
    }
    //分页查询
    @GetMapping(value = "/loadpage")
    public Page<Admin> selectPages(@RequestParam(defaultValue ="1" ) Integer PageNum,
                                   @RequestParam(defaultValue = "10") Integer PageSize,
                                   @RequestParam(defaultValue = "") String Search){
        LambdaQueryWrapper<Admin> wrapper = Wrappers.lambdaQuery();
        //判断是否为空
        if(StrUtil.isNotBlank(Search)){
            wrapper.like(Admin::getAdminname,Search);
        }
        return adminService.getSelectAdminList(PageNum,PageSize,wrapper);
    }

    //修改管理员信息
    @PutMapping(value = "/editadmin")
    public Object editAdmin(@RequestBody Admin admin){
        JSONObject json = new JSONObject();
        int result = adminService.updateAdmin(admin);
        if (result == 0){
            json.put(CODE,"0");
            json.put(MSG,"修改失败");
        }
        else{
            json.put(CODE,"1");
            json.put(MSG,"修改成功");
        }
        return json;
    }
    //删除管理员
    @DeleteMapping(value = "/{id}")
    public Object deleteAdmin(@PathVariable Integer id){
        JSONObject json = new JSONObject();
        if(adminService.deleteAdmin(id) == 0){
            json.put(CODE,"0");
            json.put(MSG,"删除失败");
        }
        else{
            json.put(CODE,"1");
            json.put(MSG,"删除成功");
        }
        return json;
    }
}